package com.ufo.store.management.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ufo.store.management.R;
import com.ufo.store.management.utils.GlideUtils;
import com.videogo.openapi.EZConstants;
import com.videogo.openapi.bean.EZCameraInfo;

import java.util.List;

/**
 * Created by taoli on 2016/10/26 0026.
 */

public class CameraSimpleRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context = null;
    private List<EZCameraInfo> mCameraInfoList = null;
    private OnClickListener mListener = null;

    public CameraSimpleRecyclerAdapter(Context context, List<EZCameraInfo> deviceInfoList) {
        this.context = context;
        mCameraInfoList = deviceInfoList;
    }


    public void setData(List<EZCameraInfo> list) {
        this.mCameraInfoList = list;
    }

    public EZCameraInfo getItem(int pos) {
        EZCameraInfo info = mCameraInfoList.get(pos);
        return info;
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }

    @Override
    public int getItemCount() {
        return mCameraInfoList.size();
    }

    class RecyclerViewHolder extends RecyclerView.ViewHolder {
        ImageView coverIv;
        ImageView playIv;
        ImageView cameraStatusIv;
        TextView cameraNameTv;
        TextView cameraLevelTv;

        public RecyclerViewHolder(View view) {
            super(view);
            coverIv = (ImageView) view.findViewById(R.id.cover_iv);
            playIv = (ImageView) view.findViewById(R.id.play_iv);
            cameraStatusIv = (ImageView) view.findViewById(R.id.camera_status_iv);
            cameraNameTv = (TextView) view.findViewById(R.id.camera_name_tv);
            cameraLevelTv = (TextView) view.findViewById(R.id.camera_level_tv);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        if (viewType == 1) {
            holder = new RecyclerViewHolder(LayoutInflater.from(
                    this.context).inflate(R.layout.camera_pageritem_online, parent,
                    false));
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        final EZCameraInfo cameraInfo = mCameraInfoList.get(position);
        if (cameraInfo != null) {
            RecyclerViewHolder holder1 = (RecyclerViewHolder) holder;
            holder1.playIv.setTag(position);
            holder1.playIv.setOnClickListener(mOnClickListener);

//            if (cameraInfo.getStatus() == 2) {
//                viewHolder.cameraStatusIv.setVisibility(View.VISIBLE);
//                viewHolder.cameraStatusTv.setText("设备离线");
//            } else
            {
                holder1.cameraStatusIv.setVisibility(View.GONE);
            }

            // 视频质量，2-高清，1-标清，0-流畅
            if (cameraInfo.getVideoLevel() == EZConstants.EZVideoLevel.VIDEO_LEVEL_FLUNET) {
                holder1.cameraLevelTv.setText("流畅");
            } else if (cameraInfo.getVideoLevel() == EZConstants.EZVideoLevel.VIDEO_LEVEL_BALANCED) {
                holder1.cameraLevelTv.setText("平衡");
            } else if (cameraInfo.getVideoLevel() == EZConstants.EZVideoLevel.VIDEO_LEVEL_HD) {
                holder1.cameraLevelTv.setText("高清");
            }

            holder1.cameraNameTv.setText(cameraInfo.getCameraName());

            String imgUrl = cameraInfo.getCameraCover();
            if( !TextUtils.isEmpty(imgUrl)){
                GlideUtils.LoadRoundImage(context, imgUrl, 10, holder1.coverIv);
            }

//            if (cameraInfo.getCameraName().contains("1号收银台")) {
//                ImageLoader.getInstance().displayImage("drawable://" + R.drawable.pic1, holder1.coverIv, defaultOptions);
//            } else if (cameraInfo.getCameraName().contains("2号收银台")) {
//                ImageLoader.getInstance().displayImage("drawable://" + R.drawable.pic2, holder1.coverIv, defaultOptions);
//            }  else if (cameraInfo.getCameraName().contains("店仓库")) {
//                ImageLoader.getInstance().displayImage("drawable://" + R.drawable.pic3, holder1.coverIv, defaultOptions);
//            }  else if (cameraInfo.getCameraName().contains("店门口")) {
//                ImageLoader.getInstance().displayImage("drawable://" + R.drawable.pic4, holder1.coverIv, defaultOptions);
//            } else if (cameraInfo.getCameraName().contains("视频5@DS-7808HW")) {
//                ImageLoader.getInstance().displayImage("drawable://" + R.drawable.pic5, holder1.coverIv, defaultOptions);
//            } else if (cameraInfo.getCameraName().contains("视频6@DS-7808HW")) {
//                ImageLoader.getInstance().displayImage("drawable://" + R.drawable.pic6, holder1.coverIv, defaultOptions);
//            } else if (cameraInfo.getCameraName().contains("视频7@DS-7808HW")) {
//                ImageLoader.getInstance().displayImage("drawable://" + R.drawable.pic7, holder1.coverIv, defaultOptions);
//            } else if (cameraInfo.getCameraName().contains("视频8@DS-7808HW")) {
//                ImageLoader.getInstance().displayImage("drawable://" + R.drawable.pic8, holder1.coverIv, defaultOptions);
//            }
//            if (cameraInfo.getCameraName().contains("C2(485898386)")) {
//                ImageLoader.getInstance().displayImage("drawable://" + R.drawable.c2, holder1.coverIv, defaultOptions);
//            }
//            if (cameraInfo.getCameraName().contains("视频1@DS-7104N-SN")) {
//                ImageLoader.getInstance().displayImage("drawable://" + R.drawable.pic11, holder1.coverIv, defaultOptions);
//            } else if (cameraInfo.getCameraName().contains("视频2@DS-7104N-SN")) {
//                ImageLoader.getInstance().displayImage("drawable://" + R.drawable.pic12, holder1.coverIv, defaultOptions);
//            } else if (cameraInfo.getCameraName().contains("视频3@DS-7104N-SN")) {
//                ImageLoader.getInstance().displayImage("drawable://" + R.drawable.pic13, holder1.coverIv, defaultOptions);
//            } else if (cameraInfo.getCameraName().contains("视频4@DS-7104N-SN")) {
//                ImageLoader.getInstance().displayImage("drawable://" + R.drawable.pic14, holder1.coverIv, defaultOptions);
//            }
        }
    }

    public void setOnClickListener(OnClickListener l) {
        mListener = l;
    }

    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mListener != null) {
                int position = (Integer) v.getTag();
                switch (v.getId()) {
                    case R.id.play_iv:
                        mListener.onPlayClick(v, position);
                        break;
                }
            }
        }
    };

    public interface OnClickListener {
        void onPlayClick(View view, int position);
    }
}